package com.tansun.easycare.rule.monitor.web;

import com.github.pagehelper.PageInfo;
import com.tansun.easycare.core.config.Global;
import com.tansun.easycare.core.utils.DateUtils;
import com.tansun.easycare.core.utils.ResponseTable;
import com.tansun.easycare.rule.common.model.ResponseVO;
import com.tansun.easycare.rule.datamodel.entity.RuleDataset;
import com.tansun.easycare.rule.datamodel.web.DataModelController;
import com.tansun.easycare.rule.monitor.entity.RuleMonitorInfo;
import com.tansun.easycare.rule.monitor.service.RuleMonitorInfoService;
import com.tansun.easycare.rule.rulelog.entity.RuleLogs;
import com.tansun.easycare.rule.rulelog.service.RuleLogsService;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 监控控制层
 */
@Controller
@RequestMapping("ruleProj/monitorInfo")
public class RuleMonitorInfoController {
    private static final Logger logger = LoggerFactory.getLogger(RuleMonitorInfoController.class);
    public SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
    public SimpleDateFormat format1 = new SimpleDateFormat(
            "yyyyMMdd HH:mm:ss");
    @Autowired
    private RuleMonitorInfoService ruleMonitorInfoService;
    @Autowired
    private RuleLogsService ruleLogsService;

    /**
     *获取项目下调用编码被调用次数
     * @param projectCode
     * @return
     */
    @RequestMapping("findEnterpointCount")
    @ResponseBody
    public ResponseVO findEnterpointCount(@RequestParam("projectCode") String projectCode){
        ResponseVO responseVO = new ResponseVO();
        responseVO.setStatus(200);
        RuleMonitorInfo ruleEnterpointCount = new RuleMonitorInfo();
        ruleEnterpointCount.setProjectCode(projectCode);
        List<RuleMonitorInfo> enterpointCountList = new ArrayList<>();
        try{
            enterpointCountList = ruleMonitorInfoService.findMonitorDataByCode(ruleEnterpointCount);
            responseVO.setData(enterpointCountList);
        }catch (Exception e){
            responseVO.setStatus(500);
            responseVO.setDesDetail(e.getMessage());
            e.printStackTrace();
        }
        return responseVO;
    }

    /**
     *获取项目下调用编码被调用次数
     * @return
     */
    @RequestMapping("findEnterpointExistFinalResult")
    @ResponseBody
    public ResponseVO findEnterpointExistFinalResult(){
        ResponseVO responseVO = new ResponseVO();
        responseVO.setStatus(200);
        RuleMonitorInfo ruleEnterpointCount = new RuleMonitorInfo();
        try{
            List<RuleMonitorInfo> enterpointCountList = ruleMonitorInfoService.findListExistFinalResult(ruleEnterpointCount);
            List<String> projectCodes=new ArrayList();
            if(enterpointCountList!=null && enterpointCountList.size()>0){
                for(RuleMonitorInfo ruleMonitorInfo:enterpointCountList){
                    if(!projectCodes.contains(ruleMonitorInfo.getProjectCode())){
                        projectCodes.add(ruleMonitorInfo.getProjectCode());
                    }
                }
            }
            responseVO.setData(projectCodes);
        }catch (Exception e){
            responseVO.setStatus(500);
            responseVO.setDesDetail(e.getMessage());
            e.printStackTrace();
        }
        return responseVO;
    }

    /**
     * 获取项目下的enterpoint
     */
    @RequestMapping("findEnterpointList")
    @ResponseBody
    public ResponseVO findEnterpointList(@RequestParam("projectCode") String projectCode){
        ResponseVO responseVO = new ResponseVO();
        responseVO.setStatus(200);
        RuleMonitorInfo ruleEnterpointCount = new RuleMonitorInfo();
        ruleEnterpointCount.setProjectCode(projectCode);
        List<RuleMonitorInfo> enterpointCountList = new ArrayList<>();
        try{
            enterpointCountList = ruleMonitorInfoService.findListExistFinalResult(ruleEnterpointCount);
            responseVO.setData(enterpointCountList);
        }catch (Exception e){
            responseVO.setStatus(500);
            responseVO.setDesDetail(e.getMessage());
            e.printStackTrace();
        }

        return responseVO;
    }

    /**
     *获取enterpoint的finalResult
     * @param enterpoint
     * @return
     */
    @RequestMapping("findEnterpointFinalResult")
    @ResponseBody
    public ResponseVO findEnterpointFinalResult(@RequestParam("projectCode") String projectCode,@RequestParam("enterpoint") String enterpoint){
        ResponseVO responseVO = new ResponseVO();
        responseVO.setStatus(200);
        RuleMonitorInfo ruleEnterpointCount = new RuleMonitorInfo();
        ruleEnterpointCount.setEnterpoint(enterpoint);
        ruleEnterpointCount.setProjectCode(projectCode);
        List<RuleMonitorInfo> enterpointCountList = new ArrayList<>();
        try{
            enterpointCountList = ruleMonitorInfoService.findListExistFinalResult(ruleEnterpointCount);
            if(null!=enterpointCountList && enterpointCountList.size()>0){
//                Map<String,Integer> map=new HashMap();
//                for(RuleMonitorInfo ruleMonitorInfo:enterpointCountList){
//
//                    JSONObject  jsonObject = JSONObject.fromObject(ruleMonitorInfo.getFinalResult());
//                    Map<String,Integer> mapFinalResult=(Map<String,Integer>)jsonObject;
//                    if(mapFinalResult.size()>0){
//                        for(String key:mapFinalResult.keySet()){
//                            if(map.containsKey(key)){
//                                map.put(key,map.get(key)+mapFinalResult.get(key));
//                            }else{
//                                map.put(key,mapFinalResult.get(key));
//                            }
//                        }
//                    }
//                }

                JSONObject  jsonObject = JSONObject.fromObject(enterpointCountList.get(0).getFinalResult());
                responseVO.setData((Map<String,Integer>)jsonObject);
                //String jsonString = JSON.toJSONString(map);
            }
        }catch (Exception e){
            responseVO.setStatus(500);
            responseVO.setDesDetail(e.getMessage());
            e.printStackTrace();
        }

        return responseVO;
    }
    
    @RequestMapping("findMonitorExeData")
    @ResponseBody
    public ResponseTable<RuleLogs> findMonitorExeData(@RequestBody RuleLogs ruleLogs){

        try {
            String monitorDaysStr = Global.getConfig("monitor.days");
            Integer monitorDays = Integer.parseInt(monitorDaysStr);
            Date date = DateUtils.addDays(new Date(),  monitorDays*-1);
            Date   beginDate = getBeginTime(date);
            ruleLogs.setIssuccess("0");
//            ruleLogs.setEndDate(new Date());
//            ruleLogs.setBeginDate(beginDate);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("监控数据中获取异常详情数据出现异常，异常信息为:"+e.getMessage());
            return new ResponseTable<>();
        }
        PageInfo<RuleLogs> pageInfo = new PageInfo<>(this.ruleLogsService.findList(ruleLogs));
        ResponseTable<RuleLogs>  resVo  = new  ResponseTable<RuleLogs>(pageInfo);
        return  resVo;
    }

    //获取某天的最开始时间
    public Date getBeginTime(Date date) throws ParseException {
        String temp = this.format.format(date);
        temp += " 00:00:00";
        Date beginDate = format1.parse(temp);
        return beginDate;
    }

}
